import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: xiaotutu
 * Date: 2023-08-19
 * Time: 10:24
 */
public class Test {
    public static void main(String[] args) {
        //二分查找
        int[] array = new int[]{1,2,3,4,5,6,7,8,9,10};
        int key = 7;
        int index = binarySearch(array,key);
        System.out.println(index);
        bubbleSort(array);
        System.out.println(Arrays.toString(array));

    }

    //冒泡排序
    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length-1; i++) {
            boolean flag = false;
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j] < array[j+1]) {
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    flag = true;
                }
            }
            if(false == flag) {
                break;
            }
        }
    }

    //二分查找
    public static int binarySearch(int[] array,int key) {
        int left = 0;
        int right = array.length-1;
        while(left <= right) {
            int mid = (left+right)/2;
            if(array[mid] > key) {
                right = mid-1;
            }else if(array[mid] < key) {
                left = mid + 1;
            }else {
                return mid;
            }
        }
        return -1;
    }
}
